package cn.engage.pet.service.impl;


import cn.engage.basic.service.impl.BaseServiceImpl;
import cn.engage.pet.domain.PetType;
import cn.engage.pet.mapper.PetTypeMapper;
import cn.engage.pet.service.PetTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Vector;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author itsource
 * @since 2022-10-28
 */
@Service
public class PetTypeServiceImpl extends BaseServiceImpl<PetType> implements PetTypeService {

    @Autowired
    private PetTypeMapper petTypeMapper;


    @Override
    public List<PetType> getPetTypeTree() {
        List<PetType>  result = new ArrayList<>();
        List<PetType> petTypeList = petTypeMapper.findAll();
        Map<Long, PetType> typeMap = petTypeList.stream().collect(Collectors.toMap(PetType::getId, e -> e));
        for (PetType e : petTypeList) {
            if (e.getPid() == null){
                // 是父类
                result.add(e);
            }else {
                PetType parent = typeMap.get(e.getPid());
                if (parent == null){
                    continue;
                }
                List<PetType> children = parent.getChildren();
                children.add(e);
            }
        }
        return result;
    }
}
